<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="评测任务ID" prop="evaluationTaskId">
        <el-input v-model="formData.evaluationTaskId" placeholder="请输入评测任务ID" />
      </el-form-item>
      <el-form-item label="模型Id" prop="largeModelId">
        <el-input v-model="formData.largeModelId" placeholder="请输入模型Id" />
      </el-form-item>
      <el-form-item label="模型名称" prop="largeModelName">
        <el-input v-model="formData.largeModelName" placeholder="请输入模型名称" />
      </el-form-item>
      <el-form-item label="数据集Id" prop="datasetId">
        <el-input v-model="formData.datasetId" placeholder="请输入数据集Id" />
      </el-form-item>
      <el-form-item label="数据集名称" prop="datasetName">
        <el-input v-model="formData.datasetName" placeholder="请输入数据集名称" />
      </el-form-item>
      <el-form-item label="promptId" prop="promptId">
        <el-input v-model="formData.promptId" placeholder="请输入promptId" />
      </el-form-item>
      <el-form-item label="prompt名称" prop="promptName">
        <el-input v-model="formData.promptName" placeholder="请输入prompt名称" />
      </el-form-item>
      <el-form-item label="prompt描述" prop="promptRemark">
        <el-input v-model="formData.promptRemark" placeholder="请输入prompt描述" />
      </el-form-item>
      <el-form-item label="数据集明细id" prop="datasetItemId">
        <el-input v-model="formData.datasetItemId" placeholder="请输入数据集明细id" />
      </el-form-item>
      <el-form-item label="评测结果状态（0 正常，1失败 ）" prop="evaluateStatus">
        <el-radio-group v-model="formData.evaluateStatus">
          <el-radio
            v-for="dict in getIntDictOptions(DICT_TYPE.MODEL_EXEC_RESULT_STATUS)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="评测结果" prop="evaluateResult">
        <el-input v-model="formData.evaluateResult" placeholder="请输入评测结果" />
      </el-form-item>
      <el-form-item label="打分结果状态（0 正常，1失败 ）" prop="scoreStatus">
        <el-radio-group v-model="formData.scoreStatus">
          <el-radio
            v-for="dict in getIntDictOptions(DICT_TYPE.MODEL_EXEC_RESULT_STATUS)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="打分结果" prop="scoreResult">
        <el-input v-model="formData.scoreResult" placeholder="请输入打分结果" />
      </el-form-item>
      <el-form-item label="问题描述" prop="datasetItemQuestion">
        <el-input v-model="formData.datasetItemQuestion" placeholder="请输入问题描述" />
      </el-form-item>
      <el-form-item label="答案描述" prop="datasetItemAnswer">
        <el-input v-model="formData.datasetItemAnswer" placeholder="请输入答案描述" />
      </el-form-item>
      <el-form-item label="模型答案" prop="modelAnswer">
        <el-input v-model="formData.modelAnswer" placeholder="请输入模型答案" />
      </el-form-item>
      <el-form-item label="指标1分数" prop="indicator1Score">
        <el-input v-model="formData.indicator1Score" placeholder="请输入指标1分数" />
      </el-form-item>
      <el-form-item label="指标2分数" prop="indicator2Score">
        <el-input v-model="formData.indicator2Score" placeholder="请输入指标2分数" />
      </el-form-item>
      <el-form-item label="指标3分数" prop="indicator3Score">
        <el-input v-model="formData.indicator3Score" placeholder="请输入指标3分数" />
      </el-form-item>
      <el-form-item label="指标4分数" prop="indicator4Score">
        <el-input v-model="formData.indicator4Score" placeholder="请输入指标4分数" />
      </el-form-item>
      <el-form-item label="指标5分数" prop="indicator5Score">
        <el-input v-model="formData.indicator5Score" placeholder="请输入指标5分数" />
      </el-form-item>
      <el-form-item label="指标6分数" prop="indicator6Score">
        <el-input v-model="formData.indicator6Score" placeholder="请输入指标6分数" />
      </el-form-item>
      <el-form-item label="指标7分数" prop="indicator7Score">
        <el-input v-model="formData.indicator7Score" placeholder="请输入指标7分数" />
      </el-form-item>
      <el-form-item label="指标8分数" prop="indicator8Score">
        <el-input v-model="formData.indicator8Score" placeholder="请输入指标8分数" />
      </el-form-item>
      <el-form-item label="指标9分数" prop="indicator9Score">
        <el-input v-model="formData.indicator9Score" placeholder="请输入指标9分数" />
      </el-form-item>
      <el-form-item label="指标10分数" prop="indicator10Score">
        <el-input v-model="formData.indicator10Score" placeholder="请输入指标10分数" />
      </el-form-item>
      <el-form-item label="综合得分" prop="score">
        <el-input v-model="formData.score" placeholder="请输入综合得分" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确定</el-button>
      <el-button @click="dialogVisible = false">取消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { EvaluationTaskItemApi, EvaluationTaskItemVO } from '@/api/multiModel/evaluationtaskitem'

/** 评测任务明细 表单 */
defineOptions({ name: 'EvaluationTaskItemForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  evaluationTaskId: undefined,
  largeModelId: undefined,
  largeModelName: undefined,
  datasetId: undefined,
  datasetName: undefined,
  promptId: undefined,
  promptName: undefined,
  promptRemark: undefined,
  datasetItemId: undefined,
  evaluateStatus: undefined,
  evaluateResult: undefined,
  scoreStatus: undefined,
  scoreResult: undefined,
  datasetItemQuestion: undefined,
  datasetItemAnswer: undefined,
  modelAnswer: undefined,
  indicator1Score: undefined,
  indicator2Score: undefined,
  indicator3Score: undefined,
  indicator4Score: undefined,
  indicator5Score: undefined,
  indicator6Score: undefined,
  indicator7Score: undefined,
  indicator8Score: undefined,
  indicator9Score: undefined,
  indicator10Score: undefined,
  score: undefined,
})
const formRules = reactive({
  evaluationTaskId: [{ required: true, message: '评测任务ID不能为空', trigger: 'blur' }],
  largeModelId: [{ required: true, message: '模型Id不能为空', trigger: 'blur' }],
  largeModelName: [{ required: true, message: '模型名称不能为空', trigger: 'blur' }],
  datasetId: [{ required: true, message: '数据集Id不能为空', trigger: 'blur' }],
  datasetName: [{ required: true, message: '数据集名称不能为空', trigger: 'blur' }],
  promptId: [{ required: true, message: 'promptId不能为空', trigger: 'blur' }],
  promptName: [{ required: true, message: 'prompt名称不能为空', trigger: 'blur' }],
  promptRemark: [{ required: true, message: 'prompt描述不能为空', trigger: 'blur' }],
  datasetItemId: [{ required: true, message: '数据集明细id不能为空', trigger: 'blur' }],
  datasetItemQuestion: [{ required: true, message: '问题描述不能为空', trigger: 'blur' }],
  datasetItemAnswer: [{ required: true, message: '答案描述不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await EvaluationTaskItemApi.getEvaluationTaskItem(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as EvaluationTaskItemVO
    if (formType.value === 'create') {
      await EvaluationTaskItemApi.createEvaluationTaskItem(data)
      message.success(t('common.createSuccess'))
    } else {
      await EvaluationTaskItemApi.updateEvaluationTaskItem(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    evaluationTaskId: undefined,
    largeModelId: undefined,
    largeModelName: undefined,
    datasetId: undefined,
    datasetName: undefined,
    promptId: undefined,
    promptName: undefined,
    promptRemark: undefined,
    datasetItemId: undefined,
    evaluateStatus: undefined,
    evaluateResult: undefined,
    scoreStatus: undefined,
    scoreResult: undefined,
    datasetItemQuestion: undefined,
    datasetItemAnswer: undefined,
    modelAnswer: undefined,
    indicator1Score: undefined,
    indicator2Score: undefined,
    indicator3Score: undefined,
    indicator4Score: undefined,
    indicator5Score: undefined,
    indicator6Score: undefined,
    indicator7Score: undefined,
    indicator8Score: undefined,
    indicator9Score: undefined,
    indicator10Score: undefined,
    score: undefined,
  }
  formRef.value?.resetFields()
}
</script>